<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/7/23
 * Time: 9:46
 */

namespace app\index\controller;

use app\index\service\CasService;
use think\Controller;
use think\Db;
use think\Exception;
use think\facade\Session;

class Login extends Controller{

    private static $KEY_USER_ID = "user_id";
    private static $KEY_USER_NAME = "user_name";
    private static $KEY_COLLEGE_ID = "college_id";
    private static $KEY_COLLEGE_NAME = "college_name";
    private static $KEY_ROLE_ID = "role_id";

    static private $KEY_SESSION_TIME = 'session_gz_time_dai32opdi-0q';
    static private $KEY_SESSION_PAGE_KEY_SHOW = 'session_gz_page_show_key';
    static private $KEY_SESSION_GROUP_KEY_SHOW = 'session_gz_group_show_key';
    static private $EXPIRE_TIME = 3600;

    // 列表查询条件
    static private $LIST_CONDITION_SB = 'list_condition_sb';
    static private $LIST_CONDITION_SB_ALTER = 'list_condition_sb_alter';
    static private $LIST_CONDITION_FURN = 'list_condition_furn';
    static private $LIST_CONDITION_FURN_ALTER = 'list_condition_furn_alter';
    static private $LIST_CONDITION_BOOK = 'list_condition_book';
    static private $LIST_CONDITION_BOOK_ALTER = 'list_condition_book_alter';
    static private $LIST_CONDITION_LAND = 'list_condition_land';
    static private $LIST_CONDITION_LAND_ALTER = 'list_condition_land_alter';
    static private $LIST_CONDITION_LIQUID = 'list_condition_liquid';
    static private $LIST_CONDITION_INTANGIBLE = 'list_condition_intangible';
    static private $LIST_CONDITION_INVEST = 'list_condition_invest';
    static private $LIST_CONDITION_CONSTRUCTION = 'list_condition_construction';
    static private $LIST_CONDITION_CAR = 'list_condition_car';
    static private $LIST_CONDITION_BUILDING = 'list_condition_building';
    static private $LIST_CONDITION_BUILDING_ALTER = 'list_condition_building_alter';
    static private $LIST_CONDITION_STOCK = 'list_condition_stock';
    static private $LIST_CONDITION_STOCK_ALTER = 'list_condition_stock_alter';

    static private $LIST_CONDITION_CON_BUILDING = 'list_condition_con_building';
    static private $LIST_CONDITION_CON_SB = 'list_condition_con_sb';
    static private $LIST_CONDITION_CON_FURN = 'list_condition_con_furn';
    static private $LIST_CONDITION_CON_LAND = 'list_condition_con_land';

    public function test(){
        $data = Db::table('EMPLOYEE')->select();
        dump($data);
    }

    public function login(){
        $cas = new CasService();
        $user = $cas->getUserInfo();
        $userId = $user['userId'];
        $data = Db::table('EMPLOYEE')->where(['STAFF_ID'=>$userId])->find();
        if(empty($data)){
            return $this->error('用户不存在');
        }
        $userRoleData = Db::table('MNG_FC_USER_ROLE')->where(['USER_ID'=>$userId])->find();
        if (empty($userRoleData) or empty($userRoleData['ROLES'])) {
            return $this->error('没有权限登录');
        }

        self::setUserId($data['STAFF_ID']);
        self::setUserName($data['NAME']);
        self::setCollegeId($data['COLLEGE_ID']);
        Session::set(self::$KEY_SESSION_TIME,time());

//        $actionList = self::getUserActionList();
//        self::setActionList($actionList);

        return $this->redirect('index/index/index');
    }

    public function login2(){
        if(request()->isAjax()){
            $userId = input('userId');
            $password = input('password');
            try{
                $user = Db::table('EMPLOYEE')->where(['STAFF_ID'=>$userId,'PASSWD'=>$password])->find();
                if(empty($user)){
                    return json_encode(['code'=>'0','msg'=>'工号或密码错误']);
                }

                self::setUserId($user['STAFF_ID']);
                self::setUserName($user['NAME']);
                self::setCollegeId($user['COLLEGE_ID']);
                Session::set(self::$KEY_SESSION_TIME,time());
                return json_encode(['code'=>'200','msg'=>'登录成功','userId'=>self::getUserId()]);
            }
            catch (Exception $e){
                return json_encode(['code'=>'0','msg'=>$e->getMessage()]);
            }
        }
        else{
            $this->view->engine->layout('layout_popup');
            return $this->fetch('login');
        }
    }

    /**
     * 切换角色
     */
    public function changeRole(){
        $roleId = input('roleId');
        self::setRoleId($roleId);
        $this->redirect('index/index/index');
    }

    public function logout(){
        session(null);
        $this->redirect('index/login/login2');
//        $cas = new CasService();
//        $cas->logout();
    }

    public static function setUserId($userId){
        Session::set(self::$KEY_USER_ID,$userId);
    }

    public static function setUserName($name){
        Session::set(self::$KEY_USER_NAME,$name);
    }

    public static function setCollegeId($collegeId){
        Session::set(self::$KEY_COLLEGE_ID,$collegeId);
    }

    public static function setListConditionSb($listConditionSb) {
        Session::set(self::$LIST_CONDITION_SB,$listConditionSb);
    }

    public static function setListConditionSbAlter($listCondition) {
        Session::set(self::$LIST_CONDITION_SB_ALTER,$listCondition);
    }

    public static function setListConditionFurn($listConditionFurn) {
        Session::set(self::$LIST_CONDITION_FURN,$listConditionFurn);
    }
    public static function setListConditionFurnAlter($listCondition) {
        Session::set(self::$LIST_CONDITION_FURN_ALTER,$listCondition);
    }

    public static function setListConditionBook($listConditionBook) {
        Session::set(self::$LIST_CONDITION_BOOK,$listConditionBook);
    }
    public static function setListConditionBookAlter($listConditionBook) {
        Session::set(self::$LIST_CONDITION_BOOK_ALTER,$listConditionBook);
    }
    public static function setListConditionLand($listConditionLand) {
        Session::set(self::$LIST_CONDITION_LAND,$listConditionLand);
    }
    public static function setListConditionLandAlter($listConditionLand) {
        Session::set(self::$LIST_CONDITION_LAND_ALTER,$listConditionLand);
    }
    public static function setListConditionLiquid($listConditionLiquid) {
        Session::set(self::$LIST_CONDITION_LIQUID,$listConditionLiquid);
    }
    public static function setListConditionIntangible($listConditionIntangible) {
        Session::set(self::$LIST_CONDITION_INTANGIBLE,$listConditionIntangible);
    }
    public static function setListConditionInvest($listConditionInvest) {
        Session::set(self::$LIST_CONDITION_INVEST,$listConditionInvest);
    }
    public static function setListConditionConstruction($listConditionConstruction) {
        Session::set(self::$LIST_CONDITION_CONSTRUCTION,$listConditionConstruction);
    }
    public static function setListConditionCar($listConditionCar) {
        Session::set(self::$LIST_CONDITION_CAR,$listConditionCar);
    }
    public static function setListConditionBuilding($listCondition) {
        Session::set(self::$LIST_CONDITION_BUILDING,$listCondition);
    }
    public static function setListConditionBuildingAlter($listCondition) {
        Session::set(self::$LIST_CONDITION_BUILDING_ALTER,$listCondition);
    }
    public static function setListConditionStock($listCondition) {
        Session::set(self::$LIST_CONDITION_STOCK,$listCondition);
    }
    public static function setListConditionStockAlter($listCondition) {
        Session::set(self::$LIST_CONDITION_STOCK_ALTER,$listCondition);
    }

    public static function setListConditionConBuilding($listCondition) {
        Session::set(self::$LIST_CONDITION_CON_BUILDING,$listCondition);
    }
    public static function setListConditionConSb($listCondition) {
        Session::set(self::$LIST_CONDITION_CON_SB,$listCondition);
    }
    public static function setListConditionConFurn($listCondition) {
        Session::set(self::$LIST_CONDITION_CON_FURN,$listCondition);
    }
    public static function setListConditionConLand($listCondition) {
        Session::set(self::$LIST_CONDITION_CON_LAND,$listCondition);
    }




    // 获取
    public static function getUserId(){
        return session(self::$KEY_USER_ID);
    }

    public static function getUserName(){
        return Session::get(self::$KEY_USER_NAME);
    }

    public static function getCollegeId(){
        return Session::get(self::$KEY_COLLEGE_ID);
    }

    public static function setRoleId($roleId){
        Session::set(self::$KEY_ROLE_ID,$roleId);
    }

    public static function getRoleId(){
        return Session::get(self::$KEY_ROLE_ID);
    }

    public static function getListConditionSb() {
        return Session::get(self::$LIST_CONDITION_SB);
    }
    public static function getListConditionSbAlter() {
        return Session::get(self::$LIST_CONDITION_SB_ALTER);
    }
    public static function getListConditionFurn() {
        return Session::get(self::$LIST_CONDITION_FURN);
    }
    public static function getListConditionFurnAlter() {
        return Session::get(self::$LIST_CONDITION_FURN_ALTER);
    }
    public static function getListConditionBook() {
        return Session::get(self::$LIST_CONDITION_BOOK);
    }
    public static function getListConditionBookAlter() {
        return Session::get(self::$LIST_CONDITION_BOOK_ALTER);
    }
    public static function getListConditionLand() {
        return Session::get(self::$LIST_CONDITION_LAND);
    }
    public static function getListConditionLandAlter() {
        return Session::get(self::$LIST_CONDITION_LAND_ALTER);
    }
    public static function getListConditionLiquid() {
        return Session::get(self::$LIST_CONDITION_LIQUID);
    }
    public static function getListConditionIntangible() {
        return Session::get(self::$LIST_CONDITION_INTANGIBLE);
    }
    public static function getListConditionInvest() {
        return Session::get(self::$LIST_CONDITION_INVEST);
    }
    public static function getListConditionConstruction() {
        return Session::get(self::$LIST_CONDITION_CONSTRUCTION);
    }
    public static function getListConditionCar() {
        return Session::get(self::$LIST_CONDITION_CAR);
    }
    public static function getListConditionBuilding() {
        return Session::get(self::$LIST_CONDITION_BUILDING);
    }
    public static function getListConditionBuildingAlter() {
        return Session::get(self::$LIST_CONDITION_BUILDING_ALTER);
    }
    public static function getListConditionStock() {
        return Session::get(self::$LIST_CONDITION_STOCK);
    }
    public static function getListConditionStockAlter() {
        return Session::get(self::$LIST_CONDITION_STOCK_ALTER);
    }
    public static function getListConditionConBuilding() {
        return Session::get(self::$LIST_CONDITION_CON_BUILDING);
    }
    public static function getListConditionConSb() {
        return Session::get(self::$LIST_CONDITION_CON_SB);
    }
    public static function getListConditionConFurn() {
        return Session::get(self::$LIST_CONDITION_CON_FURN);
    }
    public static function getListConditionConLand() {
        return Session::get(self::$LIST_CONDITION_CON_LAND);
    }



    static public function checkLogin(){
        $userId = self::getUserId();
        if(empty($userId)){
            return false;
        }

        $t1 = Session::get(self::$KEY_SESSION_TIME);
        if(time() - $t1 > self::$EXPIRE_TIME){
            return false;
        }

        Session::set(self::$KEY_SESSION_TIME,time());
        return $userId;
    }

    public static function setPageShowKey($pageShowKey){
        Session::set(self::$KEY_SESSION_PAGE_KEY_SHOW,$pageShowKey);
    }

    public static function getPageShowKey(){
        return Session::get(self::$KEY_SESSION_PAGE_KEY_SHOW);
    }

    public static function setGroupShowKey($groupShowKey){
        Session::set(self::$KEY_SESSION_GROUP_KEY_SHOW,$groupShowKey);
    }

    public static function getGroupShowKey(){
        return Session::get(self::$KEY_SESSION_GROUP_KEY_SHOW);
    }



}